package com.chenfwind.utils;


import java.security.MessageDigest;
import java.util.Date;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public abstract class EncryptUtils {

	public static final String KEY_SHA = "SHA";
	public static final String KEY_MD5 = "MD5";

	/**
	 * MAC算法可选以下多种算法
	 * 
	 * <pre>
	 * HmacMD5  
	 * HmacSHA1  
	 * HmacSHA256  
	 * HmacSHA384  
	 * HmacSHA512
	 * </pre>
	 */
	public static final String KEY_MAC = "HmacSHA1";

	/**
	 * BASE64解密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptBASE64(String key) throws Exception {
		return Base64.decode(key);
	}

	/**
	 * BASE64加密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptBASE64(byte[] key) throws Exception {
		return Base64.encode(key);
	}

	/**
	 * MD5加密
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptMD5(byte[] data) throws Exception {
		MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
		md5.update(data);
		return md5.digest();

	}

	/**
	 * SHA加密
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptSHA(byte[] data) throws Exception {
		MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
		sha.update(data);
		return sha.digest();

	}

	/**
	 * 初始化HMAC密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String initMacKey() throws Exception {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
		SecretKey secretKey = keyGenerator.generateKey();
		return encryptBASE64(secretKey.getEncoded());
	}

	/**
	 * HMAC加密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
		// ??或者进行简单的组装
		// key = key + key; ??
		
		// 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
		SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
		//此方法返回与密钥关联的算法的名称
		// Mac mac = Mac.getInstance(KEY_MAC);
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 用给定密钥初始化 Mac 对象
		mac.init(secretKey);
		return mac.doFinal(data);

	}
	
	private static String byteToHexString(byte ib) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		char[] ob = new char[2];
		ob[0] = Digit[(ib >>> 4) & 0X0f];
		ob[1] = Digit[ib & 0X0F];
		String s = new String(ob);
		return s;
	}
	
	/**
	 * HMAC加密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptHMAC(String secretKey, String plaintext) throws Exception{
		System.out.println("paintext:"+plaintext);
		byte[] key = secretKey.getBytes("utf-8");
		byte[] data = plaintext.getBytes("utf-8");
		SecretKeySpec signingKey = new SecretKeySpec(key, KEY_MAC);  
		Mac mac = Mac.getInstance(KEY_MAC);  
		mac.init(signingKey);  
		byte[] rawHmac = mac.doFinal(data); 
		StringBuffer stringBuffer = new StringBuffer();
		for (byte b : rawHmac) {
			stringBuffer.append(byteToHexString(b));
		}
		System.out.println(stringBuffer.toString());
		//生成Base64(HMAC-SHA1(secretKey, plaintext))字符串
		return encryptBASE64(stringBuffer.toString().getBytes());
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(new Date().getTime());
		System.out.println(encryptHMAC("grnwa1E7reaNj0eUuazRe581RQU=", "{\"accessTime\":\"2015-10-21 09:43:19\",\"account\":\"zhou2\",\"cityId\":\"9831a65db5220384d782e4b7\",\"cityName\":\"茂名市\",\"classId\":\"77dfc96aef89a31ba5963eab\",\"className\":\"2班\",\"countyId\":\"9c8c0e4067d068dbe1e38ac1\",\"countyName\":\"高州市\",\"gradeId\":\"fae6bb90ec4f6669126090cc\",\"gradeName\":\"小学三年级\",\"name\":\"fddfdf\",\"parentPhone\":\"13631344541\",\"password\":\"e10adc3949ba59abbe56e057f20f883e\",\"provinceId\":\"4030d2cb50bb0784c504006f\",\"provinceName\":\"广东省\",\"registerTime\":\"2015-07-13 23:43:16\",\"schoolId\":\"5a794cbbeab6b0bbe1f25f7e\",\"schoolName\":\"广东高州中学\",\"status\":1,\"statusName\":\"启用\",\"updateTime\":\"2015-10-21 09:43:19\",\"userIconSeq\":3,\"userId\":\"ef1f3ead35c9bf2fa66ea4e2\"}"));
		System.out.println(new Date().getTime());
	}
}
